package com.fjb.controller.upload;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.fjb.common.aliyun.AliYunOssConfig;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.DataSource;
import com.fjb.common.enums.file.FileStatus;
import com.fjb.common.enums.user.FileSource;
import com.fjb.common.global.GlobalConfig;
import com.fjb.common.utils.FileSizeUtil;
import com.fjb.common.utils.SerialNumberUtils;
import com.fjb.common.utils.UploadUtils;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.banner.BannerInfo;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.pojo.product.ProductCategory;
import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.user.SysUser;
import com.fjb.pojo.user.SysUserFile;
import com.fjb.service.banner.BannerInfoService;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductCategoryService;
import com.fjb.service.product.ProductInfoService;
import com.fjb.service.user.SysUserFileService;
import com.fjb.service.user.SysUserService;

/**
 * @Description:上传
 * @author hemiao
 * @time:2020年1月4日 下午8:58:44
 */
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController{
	
	@Autowired
	private SysUserFileService sysUserFileService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private BannerInfoService bannerInfoService;
	
	/**
	 * @Description:webuploader 测试
	 * @param file
	 * @param request
	 * @return
	 * JsonResult<Map<String,String>>
	 * @exception:
	 * @author: hemiao
	 * @time:2019年5月28日 下午9:30:09
	 */
    @RequestMapping(value = "/webuploader")
    @ResponseBody
    public JsonResult<Map<String,String>> webuploader(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map <String, String> resuletMap = new HashMap <String, String>();
        //图片文件类型
        String contentType = file.getContentType();   
        //图片名字
        String fileName = file.getOriginalFilename();  
        System.out.printf(contentType);
        System.out.printf(fileName);
        resuletMap.put("newFileName","");
        return new JsonResult<Map<String,String>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:商品图片上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,String>>
     * @exception:
     * @author: hemiao
     * @time:2019年6月9日 下午4:06:37
     */
    @RequestMapping(value = "/productImage")
    @ResponseBody
    public JsonResult<Map<String,Object>> productImage(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
    	// 获得文件顺序
    	String imageSort = request.getParameter("sort");
    	if(StringUtils.isBlank(imageSort)) {
    		return new JsonResult<Map<String,Object>>(null, httpCode);
    	}
    	
    	// 图片文件类型
        String contentType = file.getContentType();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = getMainUserId(user);
        
		// 获取文件大小
		long fileSize = file.getSize();
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 20) {
			httpCode.setMsg("上传文件的大小不能超过20MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_IMAGE_UF + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        String number = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        resuletMap.put("number",number);
        resuletMap.put("newFileName",newFileName);
        // 上传图片的顺序	
        resuletMap.put("sort", imageSort);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl", fileServiceUrl + newFileName);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(number);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_2.getCode());
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        Integer userFileId = suf.getId();
        resuletMap.put("userFileId", userFileId);
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:富文本上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2019年6月9日 下午10:49:03
     */
    @RequestMapping(value = "/umeditor")
    @ResponseBody
    public Map<String, String> umeditor(@RequestParam("upfile") MultipartFile upfile, HttpServletRequest request){
        HashMap<String, String> resuletMap = new HashMap<String, String>();
        resuletMap.put("state", "ERROR");
    	// 图片文件类型	
        String contentType = upfile.getContentType();
        // 图片名字	
        String fileName = upfile.getOriginalFilename(); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = getMainUserId(user);
        
		// 获取文件大小
		long fileSize = upfile.getSize();
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 20) {
			return resuletMap;
		}
		if (fileSize == 0) {
			return resuletMap;
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_UMEDITOR + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流	
		try {
			// 获取文件流
			InputStream is = upfile.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return resuletMap;
		}		
		 String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
		// 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setFileUrl(newFileName);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_4.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        resuletMap.put("state", "SUCCESS");	
        resuletMap.put("url", fileServiceUrl+newFileName);	
        return resuletMap;
    }
	
	/**
     * @Description:用户头像 上传
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,String>>
     * @exception:
     * @author: hemiao
     * @time:2019年6月9日 下午4:06:37
     */
    @RequestMapping(value = "/userPictureImage")
    @ResponseBody
    public JsonResult<Map<String,Object>> userPictureImage(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
    	// 图片文件类型	
        String contentType = file.getContentType();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = getMainUserId(user);
        
		// 获取文件大小
		long fileSize = file.getSize();
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 2) {
			httpCode.setMsg("上传文件的大小不能超过2MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		SysUserFile suf = new SysUserFile();
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_USER + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        String number = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        resuletMap.put("number",number);
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);
        LocalDateTime localDateTime = LocalDateTime.now();
        // 保存用户文件日志
        suf.setFileNumber(number);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);	
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(localDateTime);
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_1.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        // 修改用户头像
        SysUser updateUser = new SysUser();
        updateUser.setId(createUserId);	
        updateUser.setPictureUrl(newFileName);	
        updateUser.setUpdateTime(localDateTime);
        updateUser.setUpdateUserId(createUserId);
        sysUserService.updateById(updateUser);
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:上传品牌logo
     * @param file
     * @param request
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月10日 下午12:02:39
     */
    @RequestMapping(value = "/brandLogo")
    @ResponseBody
    public JsonResult<Map<String,Object>> brandLogo(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request,Integer brandId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(brandId==null) {
        	httpCode.setMsg("品牌id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 图片文件类型	
        String contentType = file.getContentType();
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = user.getMainUserId();
        
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_BRAND + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
        resuletMap.put("newFileName",newFileName);
        
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);
        
        LocalDateTime localDateTime = LocalDateTime.now();
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setId(brandId);
        brandInfo.setBrandLogo(newFileName);
        brandInfo.setUpdateTime(localDateTime);
        brandInfo.setUpdateUserId(createUserId);
        brandInfoService.updateById(brandInfo);
        
        // 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_7.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:上传产品主文件
     * @param file
     * @param request
     * @param brandId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月28日 下午4:19:07
     */
    @RequestMapping(value = "/productMainFileUrl")
    @ResponseBody
    public JsonResult<Map<String,Object>> productMainFileUrl(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request,Integer productId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(productId!=null) {
        	httpCode.setMsg("产品id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
    	// 图片文件类型	
        String contentType = file.getContentType();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = user.getMainUserId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 20) {	
			httpCode.setMsg("上传文件的大小不能超过20MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_PRODUCT + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号	
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        // 修改产品主文件	
        ProductInfo info = new ProductInfo();
        info.setId(productId);
        info.setMainFileUrl(newFileName);
        info.setUpdateTime(localDateTime);
        info.setUpdateUserId(createUserId);
        info.setContentType(contentType);
        productInfoService.updateById(info);
        
        // 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setFileUrl(newFileName);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_2.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:上传产品主文件 视频封面
     * @param file
     * @param request
     * @param productId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月28日 下午4:24:19
     */
    @RequestMapping(value = "/productVideoCoverUrl")
    @ResponseBody
    public JsonResult<Map<String,Object>> productVideoCoverUrl(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request,Integer productId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(productId!=null) {
        	httpCode.setMsg("产品id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
    	// 图片文件类型	
        String contentType = file.getContentType();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer mainUserId = getMainUserId(user);
        Integer createUserId = user.getId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}	
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_PRODUCT + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号	
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        // 修改产品主文件	
        ProductInfo info = new ProductInfo();
        info.setId(productId);	
        info.setVideoCoverUrl(newFileName);
        info.setUpdateTime(localDateTime);
        info.setUpdateUserId(createUserId);
        info.setContentType(contentType);
        productInfoService.updateById(info);
        
        // 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_2.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:删除品牌logo
     * @param request
     * @param brandId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月29日 下午10:05:35
     */
    @RequestMapping(value = "/deleteBrandLogo")
    @ResponseBody
    public JsonResult<Map<String,Object>> deleteBrandLogo(HttpServletRequest request,Integer brandId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(brandId==null) {
        	httpCode.setMsg("品牌id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
        // 图片名字
        //String fileName = request.getParameter("fileName"); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        
        // 保存用户文件日志		
        LocalDateTime localDateTime = LocalDateTime.now();
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setId(brandId);
        brandInfo.setBrandLogo("");
        brandInfo.setUpdateTime(localDateTime);
        brandInfo.setUpdateUserId(createUserId);
        brandInfoService.updateById(brandInfo);	
        return new JsonResult<Map<String,Object>>(null, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:上传产品分类图标
     * @param file
     * @param request
     * @param categoryId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月31日 下午6:47:49
     */
    @RequestMapping(value = "/categoryIcon")
    @ResponseBody
    public JsonResult<Map<String,Object>> categoryIcon(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request,Integer categoryId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(categoryId==null) {
        	httpCode.setMsg("车型id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        //图片文件类型
        String contentType = file.getContentType();  
        // 获得当前登录用户信息	
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = user.getMainUserId();
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_CATEGORY + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);
        
        LocalDateTime localDateTime = LocalDateTime.now();
        ProductCategory pc = new ProductCategory();
        pc.setId(categoryId);
        pc.setAppletIcon(newFileName);
        pc.setUpdateUserId(createUserId);
        pc.setUpdateTime(localDateTime);
        productCategoryService.updateById(pc);
        
        // 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_8.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:删除产品分类图标
     * @param request
     * @param categoryId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月31日 下午6:48:09
     */
    @RequestMapping(value = "/deleteCategoryIcon")
    @ResponseBody
    public JsonResult<Map<String,Object>> deleteCategoryIcon(HttpServletRequest request,Integer categoryId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(categoryId==null) {
        	httpCode.setMsg("车型id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
        // 图片名字
        //String fileName = request.getParameter("fileName"); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
		
        // 保存用户文件日志			
        LocalDateTime localDateTime = LocalDateTime.now();
        
        ProductCategory pc = new ProductCategory();
        pc.setId(categoryId);
        pc.setAppletIcon("");
        pc.setUpdateUserId(createUserId);
        pc.setUpdateTime(localDateTime);
        productCategoryService.updateById(pc);
        return new JsonResult<Map<String,Object>>(null, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:广告文件url
     * @param file
     * @param request
     * @param bannerId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月31日 下午10:01:07
     */
    @RequestMapping(value = "/bannerFileUrl")
    @ResponseBody
    public JsonResult<Map<String,Object>> bannerFileUrl(@RequestParam("file") MultipartFile file, 
    		HttpServletRequest request,Integer bannerId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(bannerId==null) {
        	httpCode.setMsg("广告id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
    	Map <String, Object> resuletMap = new HashMap <String, Object>();
    	String contentType = file.getContentType();
        // 图片名字
        String fileName = file.getOriginalFilename(); 
        // 获得当前登录用户信息	
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        Integer mainUserId = user.getMainUserId();
        
		// 获取文件大小
		long fileSize = file.getSize();
		if (fileSize == 0) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		double newFileSize = FileSizeUtil.formetFileSize(fileSize, FileSizeUtil.SIZETYPE_MB);
		if (newFileSize > 5) {	
			httpCode.setMsg("上传文件的大小不能超过5MB");
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		// 新的文件名称	
		String newFileName = GlobalConfig.FILE_CATEGORY + UploadUtils.getNewFileName(fileName);
		OSSClient ossClient = AliYunOssConfig.getOSSClient();
		// 获取文件流
		try {
			// 获取文件流
			InputStream is = file.getInputStream();
			ossClient.putObject(GlobalConfig.OSS_BUCKET_NAME, newFileName, is);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult<Map<String, Object>>(null, HttpCode.EXCEPTIONS);
		}
		// 创建编号
        resuletMap.put("newFileName",newFileName);
        String fileServiceUrl = GlobalConfig.FILE_SERVICE_LOCATION;
        resuletMap.put("fileUrl",  fileServiceUrl+ newFileName);

        LocalDateTime localDateTime = LocalDateTime.now();
        BannerInfo bi = new BannerInfo();
        bi.setId(bannerId);
        bi.setFileUrl(newFileName);
        bi.setUpdateUserId(createUserId);
        bi.setUpdateTime(localDateTime);
        bannerInfoService.updateById(bi);
        
        // 创建编号
        String fileNumber = SerialNumberUtils.createNumber(SerialNumberUtils.USER_FILE, mainUserId);
        // 保存用户文件日志
        SysUserFile suf = new SysUserFile();
        suf.setFileNumber(fileNumber);
        suf.setMainUserId(mainUserId);
        suf.setFileSize(newFileSize);
        suf.setContentType(contentType);
        suf.setFileUrl(newFileName);	
        suf.setFileServiceUrl(fileServiceUrl);
        suf.setUserId(mainUserId);
        suf.setCreateUserId(createUserId);
        suf.setCreateTime(LocalDateTime.now());
        suf.setFileStatus(FileStatus.STATUS_1.getCode());
        suf.setFileSource(FileSource.SOURCE_9.getCode());
        suf.setDataSource(DataSource.WINDOS.getCode());
        sysUserFileService.save(suf);		
        return new JsonResult<Map<String,Object>>(resuletMap, HttpCode.SUCCESS);
    }
    
    /**
     * @Description:删除广告文件
     * @param request
     * @param categoryId
     * @return
     * JsonResult<Map<String,Object>>
     * @exception:
     * @author: hemiao
     * @time:2020年1月31日 下午9:58:27
     */
    @RequestMapping(value = "/deleteBanner")
    @ResponseBody
    public JsonResult<Map<String,Object>> deleteBanner(HttpServletRequest request,Integer bannerId){
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        if(bannerId==null) {
        	httpCode.setMsg("广告id不能为空");
        	return new JsonResult<Map<String,Object>>(null, httpCode);
        }
        // 图片名字
        //String fileName = request.getParameter("fileName"); 
        // 获得当前登录用户信息
        LoginUserInfo userInfo = getUserInfo(request);
        SysUser user = userInfo.getSysUser();
        Integer createUserId = user.getId();
        
        // 保存用户文件日志		
        LocalDateTime localDateTime = LocalDateTime.now();
        
        BannerInfo bi = new BannerInfo();
        bi.setId(bannerId);
        bi.setFileUrl("");
        bi.setUpdateUserId(createUserId);
        bi.setUpdateTime(localDateTime);
        bannerInfoService.updateById(bi);
        return new JsonResult<Map<String,Object>>(null, HttpCode.SUCCESS);
    }
}
